
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  pool.tablename;

import java.io.RandomAccessFile;
import java.sql.ResultSet;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
import java.util.Vector;

import query.condition.typereport.Typereport;
import query.query.Queryinterface;
import query.query.Resultset;

import property.Order;

import control.*;
import dbmanager.DBManager;
import dbmanager.GetIdName;

import type.Report;
import utility.CapitalChar;
import utility.ConvertToLower;
import utility.Index;
import utility.Splitstring;
/**
 * 
 * @author osc36
 *   report_master 
 */
public class Report_master extends SubPool
{
   Hashtable<Object, Object> ReportMap = new Hashtable<Object, Object>();
   String buffer[]; 
   String reportname;
   String QUERY="query"; 	

	@Override
	public StringBuffer getInsert() 
	{
		return null;
	}

	@Override
	public StringBuffer getUpdate() 
	{
		return null;
	}

	@Override
	public StringBuffer getView() 
	{
		
		Queryinterface query;
		String package_name ="query.query.";
		Order order =new  Order();
		Hashtable<Object,Object> table =order.getSequences(gin.getId(QUERY));
		ReportMap = ConvertToLower.convertHashKey(ReportMap);
		String Querybuffer[]={};
		for(int i=0;i<table.size();i++)
		{	
			try
			{
				String Classname = table.get(Integer.toString(i+1)).toString().toLowerCase();
				String value = ReportMap.get(Classname).toString();	
				if(ReportMap.containsKey(Classname) && value.length()>0)
				{					
					Class c =Class.forName(package_name+CapitalChar.makeFirstCharCapital(Classname));
					query = (Queryinterface) c.newInstance();
					query.initPool();
					ResultSet rs = DBManager.getSelect("SELECT * FROM command where msgtype='report_master' and message='"+Classname +"' and process='view' order by order1");
					char ch='0';
					if(rs.next())
					{
						ch = rs.getString("spchar").toCharArray()[0];
					}	
					query.setViewObject(value.toString().replace("\"","").substring(Classname.length()+1),ch);
					query.settablename("report_master");
					query.setQuerybuffer(Querybuffer);
					query.setbuffer(buffer);
					query.setReportName(reportname);
					query.addReport();
					buffer =query.getbuffer();
					Querybuffer = query.getQuerybuffer();
				}
			} 
			catch (ClassNotFoundException e) 
			{
				e.printStackTrace();
			} 
			catch (InstantiationException e)
			{
				e.printStackTrace();
			} 
			catch (Exception e) 
			{
				
			}
		}
		StringBuffer Finalbuffer =new StringBuffer(); 
		for(int j=0;j<buffer.length;j++)
			Finalbuffer.append(buffer[j]+"\n");
		return Finalbuffer;
		
	}
	/** initialize() method is initialize the basic properties of report_master class.
	 *  like condition,report,field,join which stores the details of master_table entry.
	 *  @return ErrorMap, if any, which stores the Property_details map named reportmap, otherwise
	 *  @return MasterMap.    
	 **/
	
	public Hashtable<Object, Object> initialize() 
	{
		
		try 
		{	
			/*String 	userMessage = message;
			SpecialCharacter cp= new SpecialCharacter();
			int indexofspchar =cp.getNextSpChar(1, userMessage.toCharArray());
		    reportname = userMessage.substring(1, indexofspchar);
			userMessage = userMessage.substring(indexofspchar, userMessage.length());
			indexofspchar=0;*/			
			try 
			{
				String value = message;
				ResultSet rs = DBManager.getSelect("SELECT * FROM command where msgtype='report_master' and process='view'  and message='query' order by order1");
				int indexofnextspchar=1;
				while(rs.next() && value.length()>0)
				{
					String spchar =rs.getString("spchar"); 
					String Fieldname =rs.getString("fieldname");
					if(value.indexOf(spchar)!=-1)
					{
						indexofnextspchar = Index.indexOf(value, spchar, 1);
						if(indexofnextspchar==-1)
								indexofnextspchar = value.length();
						String newvalue="";
						if(!rs.isLast())
							 newvalue = value.substring(1, indexofnextspchar);
						else
							newvalue =value;
						ReportMap.put(Fieldname, newvalue);
						value = value.substring(indexofnextspchar);
					}
					else
					{
						ReportMap.put(Fieldname, value);
						value ="";
					}
				}
			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		} catch (Exception e) 
		{
			e.printStackTrace();
		}
/*
		Report report = new Report(sqlDB,gin.getId(reportname));
		String typeofreport = report.GettypeofReport();
		Hashtable<Object, Object> table = report.getReportMap();
		Typereport type;
		try 
		{
			String Value="";
			String KEY="key";
			if(ReportMap.containsKey(KEY))
				Value = ReportMap.get(KEY).toString().substring(KEY.length()+1);
			Class c =Class.forName("combinereport.condition.typereport."+CapitalChar.makeFirstCharCapital(typeofreport));
			type =(Typereport) c.newInstance();
			type.setData("", Value, sqlDB, table);
			String path=type.getPath(reportname);
			RandomAccessFile raf =new RandomAccessFile(path,"r");
			StringBuffer str =new StringBuffer();
			String line;
			while((line=raf.readLine())!=null)
			{
				str.append(line+"\n");					
			}
			buffer=str.toString().split("\n");
		} 
		catch (Exception e1) 
		{
			e1.printStackTrace();
		}
*/		return ReportMap;
		
	}

	
	public Hashtable<Object, Object> setPreprocess() 
	{
		return null;
	}

	public Hashtable<Object, Object> getBasicHashtable() 
	{	
		return null;
	}

	public Hashtable<Object, Object> getDetailHashtabel() 
	{
		return null;
	}

	public Hashtable<Object, Object> getHashtabel() 
	{
		return null;
	}

	public String getProceess() 
	{
		return null;
	}

	public String getTable() 
	{
		return null;
	}

	public String getType() 
	{
		return null;
	}

	public Vector<Object> getpropertyVector() 
	{
		return null;
	}

	public void setHashtable(Hashtable<Object, Object> table) 
	{
		
	}

	public void setPropertyVector(Vector<Object> propertydetails) 
	{
		
	}

	
	public StringBuffer getDelete() 
	{
		return null;
	}
   
	public static void main(String[] args) 
	{
		Report_master report_master = new Report_master();
		report_master.initPool();
		report_master.message= "*master_table!itemcode|type,=,item";
		report_master.initialize();
		report_master.getView();
		for(int i=0;i<report_master.buffer.length;i++)
		{
			System.out.println(report_master.buffer[i]);
		}
	}
}
